﻿using System.Collections.Generic;
using System.Web.UI;
using System.Web.Mvc;
using Liber.WEB;
using Liber;

namespace SoBusiness.Controllers
{
    public class AdminController:LiberController
    {        
        /// <summary>
        /// 公司信息管理
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin","Company","Admin")]  //表示需要Admin应用，公司对象的监管权限
        public ViewResult Company()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 组织架构管理
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin", "Organize", "Admin")]
        public ViewResult Organize()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 企业员工账号管理
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin", "Account", "Admin")]
        public ViewResult Accounts()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 权限控制
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin", "Authority", "Admin")]
        public ViewResult Authority()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 企业门户管理
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin", "Portal", "Admin")]
        public ViewResult Portal()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 开放接口管理
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin", "OpenAPI", "Admin")]
        public ViewResult OpenAPI()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 平台应用管理
        /// </summary>
        /// <returns></returns>
        [AppAurhority("Admin", "CloudApp", "Admin")]
        public ViewResult CloudApp()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        public JsonResult GetOrganizeTree(long company=-1)
        {
            return Json(API.Request("OrganizeSvc@GetOrganizeTree")
                           .Set("companyid",company)
                           .WaitResult());
        }

        public JsonResult GetOrgPositions(long objid = -1)
        {
            var rtn = API.Request("OrganizeSvc@GetOrgPositions")
                       .Set("orgid", objid)
                       .WaitResult();
            if (rtn.Data!=null)
            {
                rtn.Data.Set("orgidstr", objid.ToString());
            }

            return Json(rtn);
        }

        public JsonResult GetPositionDetail(long posid = -1)
        {
            return Json(API.Request("OrganizeSvc@GetPositionDetail")
                           .Set("posid",posid)
                           .WaitResult());
        }

        public JsonResult UpdateOrgInfo(long objid,long parentid,string name,string remark)
        {
            return Json(API.Request("OrganizeSvc@UpdateOrgInfo")
                           .Set("objid", objid)
                           .Set("parentid", parentid)
                           .Set("name", name)
                           .Set("remark", remark).WaitResult());
        }

        public JsonResult NewOrganize(long parentid, string name,string remark)
        {
            if(string.IsNullOrWhiteSpace(name))
            {
                return Json(FnResult.Failure("AdminController", "RequiredArgs").SetError("name", "name is empty"));
            }
            return Json(API.Request("OrganizeSvc@CreateOrganize")
                      .Set("parentid", parentid)
                      .Set("name", name)
                      .Set("remark", remark)
                      .WaitResult());
        }

        //[AppAurhority("Admin","Organize","Admin")]
        public JsonResult NewPosition(long orgid,long parentid,string name,string remark)
        {
            if (string.IsNullOrEmpty(name))
            {
                return Json(FnResult.Failure("AdminController", "RequiredArgs").SetError("name", "name is empty"));
            }

            if (orgid==-1)
            {
                return Json(FnResult.Failure("AdminController", "RequiredArgs").SetError("orgid", "orgid is null"));
            }

            return Json(API.Request("OrganizeSvc@CreatePosition")
                           .Set("parentid", parentid)
                           .Set("orgid", orgid)
                           .Set("name", name)
                           .Set("remark", remark)
                           .WaitResult());
        }

        public JsonResult UpdatePosition(long idstr,string name,string remark)
        {
            if (string.IsNullOrEmpty(name))
            {
                return Json(FnResult.Failure("AdminController", "RequiredArgs").SetError("name", "name is empty"));
            }

            return Json(API.Request("OrganizeSvc@UpdatePosition")
                           .Set("posid", idstr)
                           .Set("name", name)
                           .Set("remark", remark)
                           .WaitResult());
        }

        public JsonResult DelPosition(long posid)
        {
            return Json(API.Request("OrganizeSvc@DelPosition")
                           .Set("posid", posid)
                           .WaitResult());
        }

        //若不指定，则只获取第一个所属公司或创建的公司
        public JsonResult GetCompanyInformation(long companyid)
        {
            return Json(API.Request("OrganizeSvc@GetCompany")
                      .Set("companyid", companyid)
                      .WaitResult());
        }

        public JsonResult UpdateCompanyInfo()
        {
            long id = Request.Params.TryGetLong("idstr");
            string name = Request.Params.TryGet("name");
            string logourl = Request.Params.TryGet("logourl");
            string address = Request.Params.TryGet("address");
            string officephone = Request.Params.TryGet("officephone");
            string industry = Request.Params.TryGet("industry");
            string website = Request.Params.TryGet("website");

            return Json(API.Request("OrganizeSvc@UpdateCompanyInfo")
                           .Set("name", name)
                           .Set("logourl", logourl)
                           .Set("address", address)
                           .Set("officephone", officephone)
                           .Set("industry", industry)
                           .Set("website", website)
                           .Set("id",id)
                           .WaitResult());
        }

        public JsonResult AddPositionAuth(long posid,long authorityid)
        {
            return Json(API.Request("AuthoritySvc@AddRoleAuth")
                           .Set("roleid", posid)
                           .Set("authid", authorityid)
                           .WaitResult());
        }

        public JsonResult RemovePositionAuth(long posid,long authorityid)
        {
            return Json(API.Request("AuthoritySvc@RemoveRoleAuth")
               .Set("roleid", posid)
               .Set("authid", authorityid)
               .WaitResult());
        }

        /// <summary>
        /// 获取当前用户所属顶级公司下待审核的账号加入申请
        /// </summary>
        /// <returns></returns>
        public JsonResult GetUnapproveApplies()
        {
            return Json(API.Request("OrganizeSvc@GetAcntApplication")
                           .WaitResult());
        }

        public JsonResult GetCompanyAcnts(long company = -1)
        {
            return Json(API.Request("OrganizeSvc@GetCompanyAcnts")
                           .Set("companyid", company)
                           .WaitResult());
        }

        public JsonResult ApproveAcntApplication(long objid,long acntid,long pos=-1,string result="",string feedback="")
        {
            var r = API.Request("OrganizeSvc@AcntApprove")
                     .Set("applyid", objid)
                     .Set("acntid",acntid)
                     .Set("position", pos)
                     .Set("result",result)
                     .Set("feedback", feedback)
                     .WaitResult();
            return r.IsPositive ? Json(FnResult<DyObject>.Sucess(new DyObject().Set("idstr", objid.ToString()))) : Json(FnResult.Failure(r.ErrorSrc, r.ErrorCode).SetError("idstr", objid.ToString()));
        }

        public JsonResult SetAcntPosition(long acnt = -1,long pos=-1,string postype = "Acnt-Mainposition")
        {
            return Json(API.Request("OrganizeSvc@SetAcntPosition")
                           .Set("acnt", acnt)
                           .Set("pos", pos)
                           .Set("postype", postype)
                           .WaitResult());
        }
    }
}